#include<bits/stdc++.h>
#define ll long long
#define int ll
#define L x<<1
#define R x<<1|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define Root 1,1,n
#define OK l>=Ll&&r<=Rr
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define pb push_back
#define ull unsigned long long
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned int 
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57) s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
const int N =4e2+5,M=4e5+5,inf=2147000000;
const double eps=1e-6;
const ll mod=147744151,llf=1e18;
using namespace std;
ll ans=-llf;
int d[N],n,h[N],to[N<<1],nxt[N<<1],a[N],cnt;
inline void add(int a,int b){
	d[a]++,to[++cnt]=b,nxt[cnt]=h[a],h[a]=cnt;
}
bool v[14],used[14];
int b[14];ll c[14];
inline void sol(int x){
	v[x]=1;
	e(x)if(!v[y])c[y]+=c[x];
}
inline void baoli(int pos){
	if(pos==n+1){
		memset(v,0,sizeof(v));
		ll now=0;
		rep(i,1,n)c[i]=a[i];
		rep(i,1,n){
			now+=c[b[i]];
			sol(b[i]);
		}
		ans=max(ans,now);
		return;
	}
	rep(i,1,n){
		if(!used[i]){
			used[i]=1,b[pos]=i;
			baoli(pos+1);
			used[i]=b[pos]=0;
		}
	}
}
inline void subt1(){
	baoli(1);
	cout <<ans;
}
ll w[N];
inline void subtb(){
	rep(i,2,n)w[i-1]=a[i];
	sort(w+1,w+n);
	ll sum=0,pre=0;
	rep(i,1,n-1)sum+=w[i];
	ans=max(sum*2LL+a[1],a[1]*n+sum);
	per(i,n-1,1){
		pre+=w[i];
		ans=max(ans,pre*(i+1)+a[1]*i+sum-pre);
	}
	pre=0;
	rep(i,1,n-1){
		pre+=w[i];
		ans=max(ans,pre*(n-i+1)+a[1]*(n-i)+sum-pre); 
	}
	cout <<ans;
}
signed main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	n=read();
	rep(i,1,n)a[i]=read();
	for(int i=1,x,y;i^n;i++)x=read(),y=read(),add(x,y),add(y,x);
	if(n<=10){
		subt1();
		return 0;
	}
	if(d[1]==n-1){
		subtb();
		return 0;
	}
	return 0;
}
